package com.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * 349. 两个数组的交集
 * 使用三个Set取交集
 *
 * @author fy
 * @date 2022/4/6 8:48
 */
public class Solution349 {

    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            set1.add(nums1[i]);
        }
        for (int i = 0; i < nums2.length; i++) {
            set2.add(nums2[i]);
        }
        Set<Integer> set = new HashSet<>();
        for (Integer item : set1) {
            if (set2.contains(item)) {
                set.add(item);
            }
        }
        int[] res = new int[set.size()];
        int idx = 0;
        for (Integer item : set) {
            res[idx++] = item;
        }
        return res;
    }

}
